package fr.enseirbmatmeca.apms.hibernate.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import fr.enseirbmatmeca.apms.hibernate.dao.ProjectDAO;
import fr.enseirbmatmeca.apms.spring.model.project.Project;
import fr.enseirbmatmeca.apms.spring.model.project.ProjectCollection;
import fr.enseirbmatmeca.apms.spring.model.project.ProjectGroup;
import fr.enseirbmatmeca.apms.spring.model.user.User;

/**
 * Implementation for project DAOs
 * @author ffleurey, coudar
 */
@Repository("projectDao")
public class ProjectDAOImpl implements ProjectDAO {

    protected static Log logger = LogFactory.getLog(ProjectDAOImpl.class);
    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public Project createProject(Project project) {
    	Session session = sessionFactory.getCurrentSession();		
		session.persist(project);	
        return project;
    }

    public Project getProject(User user) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public List<Project> getProjects(User user) {
        switch (user.getType()) {
            case User.ADMINISTRATOR:
                // Admin is involved in all projects
                //int size = sessionFactory.getCurrentSession().createCriteria(Project.class).list().size();
                return sessionFactory.getCurrentSession().createCriteria(Project.class).list();
            case User.USER:
                break;
            default:
                break;
        }
        return null;
    }

    public Project updateProject(Project project) {
        // TODO Auto-generated method stub
        return null;
    }

	public ProjectCollection createProjectCollection(ProjectCollection pc) {
		Session session =sessionFactory.getCurrentSession();		
		session.persist(pc);	
		return pc;
	}

	public ProjectGroup createProjectGroup(ProjectGroup pg) {
		Session session =sessionFactory.getCurrentSession();		
		session.persist(pg);	
		return pg;
	}
    
    
}
